<template>
  <div>
    <el-table
        stripe
        border
        :data="lists"
        style="width: 100%">
        <el-table-column
          label="ID"
          prop="id">
        </el-table-column>
        <el-table-column
          label="券码"
          prop="code">
        </el-table-column>
        <el-table-column
          label="类型"
          prop="name">
        </el-table-column>
        <el-table-column
          label="面值"
          prop="discount_value">
        </el-table-column>
        <el-table-column
          label="生效时间"
          prop="start_time">
        </el-table-column>
        <el-table-column
          label="结束时间"
          prop="end_time">
        </el-table-column>
        <el-table-column
          label="拥有者"
          prop="username">
        </el-table-column>
        <el-table-column
          label="拥有数量"
          prop="num">
        </el-table-column>
        <el-table-column
          label="库存数量"
          prop="remain_count">
        </el-table-column>
        <el-table-column
          align="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"  >删除</el-button>
          </template>
        </el-table-column>
    </el-table>
      <div class="page">
        <el-pagination
        layout="prev, pager, next"
        :total="total"
        @prev-click="current_change"
        @next-click="current_change"
        @current-change="current_change">
      </el-pagination>
      </div>
  </div>
</template>
<script>
import { all_coupon_list, del_coupon } from '@/api/coupon_api';
export default{
  data(){
    return{
      lists:[],
      pageMap:{
        now_page:1,
        limit:10,
      },
      total:0,
    }
  },
  methods:{
    current_change(page){
      this.pageMap.now_page = page
      this.order_info()
    },
    handleDelete(index,row){
       this.$confirm('是否删除该用的优惠券', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {data:res} = await del_coupon({id:row.id})
          this.$message({
            type: res.code==200?'success':'error',
            message: res.msg
          });
          if(res.code==200)this.order_info()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
  async order_info(){
    const {data:res} = await all_coupon_list(this.pageMap)
    this.lists = res.data.list
    console.log(this.lists);
    
    this.total = res.data.total
  },
  },
  created(){
    this.order_info()
  }
}
</script>
<style lang="scss" scoped>
.page{
  float: right;
  margin-top: 2%;
}
.btn{
  margin-bottom: 2%;
}
</style>